package com.cqupt.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqupt.model.dto.AdminLoginDto;
import com.cqupt.model.dto.AdminRegDto;
import com.cqupt.model.dto.AdminUpdatePsdDto;
import com.cqupt.model.dto.SceneDto;
import com.cqupt.model.pojo.*;
import com.cqupt.model.vo.PageVo;
import com.cqupt.model.vo.Result;
import com.cqupt.model.vo.ResultCodeEnum;
import com.cqupt.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/admin")
@Tag(name = "管理员模块")
public class AdminController {
    @Resource
    private AdminService adminService;

    @Resource
    private BookCategoryService bookCategoryService;

    @Resource
    private BookService bookService;

    @Resource
    private SceneCategoryService sceneCategoryService;


    @Resource
    private SceneService sceneService;


    @Resource
    private ArticleCategoryService articleCategoryService;

    @Resource
    private ArticleService articleService;

    @Resource
    private SysUserService sysUserService;

    @PostMapping("/login")
    @Operation(summary = "登录")
    public Result login(@RequestBody AdminLoginDto adminLoginDto) {

        return adminService.login(adminLoginDto);
    }

    @PostMapping("/register")
    @Operation(summary = "注册")
    public Result register(@RequestBody AdminRegDto adminLoginDto) {
        return adminService.register(adminLoginDto);
    }


    @Operation(summary = "退出登录")
    @GetMapping("/logout")
    public Result logout(@RequestHeader String token) {
        return adminService.logout(token);
    }

    @Operation(summary = "修改密码")
    @PostMapping("/password")
    public Result updatePassword(@RequestBody AdminUpdatePsdDto adminUpdatePsdDto, @RequestHeader String token) {
        return adminService.updatePassword(adminUpdatePsdDto, token);
    }


    @Operation(summary = "查询词书分类")
    @GetMapping("/book/category")
    public Result getBookCategory() {
        List<BookCategory> all = bookCategoryService.all();
        return Result.build(all, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "删除词书分类")
    @DeleteMapping("/book/category")
    public Result delBookCategory(@RequestParam("id") Integer id) {
        bookCategoryService.removeById(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改词书分类")
    @PutMapping("/book/category")
    public Result updateBookCategory(@RequestBody BookCategory bookCategory) {
        bookCategoryService.updateById(bookCategory);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "新增词书分类")
    @PostMapping("/book/category")
    public Result insertBookCategory(@RequestParam(name = "name") String name) {
        BookCategory bookCategory = new BookCategory();
        bookCategory.setName(name);
        bookCategoryService.save(bookCategory);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "查询所有词书")
    @GetMapping("/allBook")
    public Result getBook(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {

        return Result.build(bookService.getBookByPage(pageNum, pageSize), ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "根据词书分类查询词书")
    @GetMapping("/book")
    public Result getBook(@RequestParam("categoryId") Integer categoryId,
                          @RequestParam("pageNum") Integer pageNum,
                          @RequestParam("pageSize") Integer pageSize) {
        PageVo<Book> page = bookService.getByCategoryId(categoryId, pageNum, pageSize);
        return Result.build(page, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "删除词书")
    @DeleteMapping("/book")
    public Result delBook(@RequestParam("id") Integer id) {
        Book book = bookService.getById(id);
        book.setStatus(0);
        bookService.updateById(book);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改词书")
    @PutMapping("/book")
    public Result updateBook(@RequestBody Book book) {
        bookService.updateById(book);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "新增词书")
    @PostMapping("/book")
    public Result insertBook(@RequestBody Book book) {
        bookService.save(book);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "查询情景分类")
    @GetMapping("/scene/category")
    public Result getSceneCategory() {
        List<SceneCategory> all = sceneCategoryService.list();
        return Result.build(all, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "删除情景分类")
    @DeleteMapping("/scene/category")
    public Result delSceneCategory(@RequestParam("id") Integer id) {
        sceneCategoryService.removeById(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改情景分类")
    @PutMapping("/scene/category")
    public Result updateSceneCategory(@RequestBody SceneCategory sceneCategory) {
        sceneCategoryService.updateById(sceneCategory);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "新增情景分类")
    @PostMapping("/scene/category")
    public Result insertSceneCategory(@RequestParam("name") String name) {
        SceneCategory sceneCategory = new SceneCategory();
        sceneCategory.setName(name);
        sceneCategoryService.save(sceneCategory);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "查询所有情景")
    @GetMapping("/allScene")
    public Result getScene(@RequestParam("pageNum") Integer pageNum,
                           @RequestParam("pageSize") Integer pageSize) {
        PageVo<Scene> pageVo = new PageVo<>();
        Page<Scene> page = new Page<>(pageNum, pageSize);
        List<Scene> all = sceneService.page(page).getRecords();
        pageVo.setRecords(all);
        pageVo.setTotal(page.getPages());
        return Result.build(pageVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "根据情景分类查询情景")
    @GetMapping("/scene")
    public Result getScene(@RequestParam("categoryId") Integer categoryId,
                           @RequestParam("pageNum") Integer pageNum,
                           @RequestParam("pageSize") Integer pageSize) {
        return Result.build(sceneService.sceneList(categoryId, pageNum, pageSize), ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "根据情景id查询情景详细信息")
    @GetMapping("/detail")
    public Result getSceneDetail(@RequestParam("id") Integer id) {
        return Result.build(sceneService.getSceneDetail(id), ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "删除情景")
    @DeleteMapping("/scene")
    public Result delScene(@RequestParam("id") Integer id) {
        sceneService.delScene(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改情景")
    @PutMapping("/scene")
    public Result updateScene(@RequestBody SceneDto sceneDto) {
        sceneService.updateScene(sceneDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "新增情景")
    @PostMapping("/scene")
    public Result insertScene(@RequestBody SceneDto sceneDto) {
        sceneService.insertBook(sceneDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "查询文章分类")
    @GetMapping("/article/category")
    public Result getArticleCategory() {
        List<ArticleCategory> all = articleCategoryService.list();
        return Result.build(all, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "删除文章分类")
    @DeleteMapping("/article/category")
    public Result delArticleCategory(@RequestParam("id") Integer id) {
        articleCategoryService.removeById(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改文章分类")
    @PutMapping("/article/category")
    public Result updateArticleCategory(@RequestBody ArticleCategory articleCategory) {
        articleCategoryService.updateById(articleCategory);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "新增文章分类")
    @PostMapping("/articl/category")
    public Result insertArticleCategory(@RequestParam("name") String name) {
        ArticleCategory articleCategory = new ArticleCategory();
        articleCategory.setName(name);
        articleCategoryService.save(articleCategory);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "查询所有文章")
    @GetMapping("/allArticle")
    public Result getArticle(@RequestParam("pageNum") Integer pageNum,
                             @RequestParam("pageSize") Integer pageSize) {
        Page<Article> page = new Page<>(pageNum, pageSize);
        List<Article> all = articleService.page(page).getRecords();
        PageVo<Article> pageVo = new PageVo<>();
        pageVo.setRecords(all);
        pageVo.setTotal(page.getPages());
        return Result.build(pageVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "根据文章分类查询文章")
    @GetMapping("/article")
    public Result getArticle(@RequestParam("categoryId") Integer categoryId,
                             @RequestParam("pageNum") Integer pageNum,
                             @RequestParam("pageSize") Integer pageSize) {
        Page<Article> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Article::getCategoryId, categoryId);
        List<Article> all = articleService.page(page, wrapper).getRecords();
        PageVo<Article> pageVo = new PageVo<>();
        pageVo.setRecords(all);
        pageVo.setTotal(page.getPages());
        return Result.build(pageVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "删除文章")
    @DeleteMapping("/article")
    public Result delArticle(@RequestParam("id") Integer id) {
        articleService.removeById(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改文章")
    @PutMapping("/article")
    public Result updateArticle(@RequestBody Article article) {
        articleService.updateById(article);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "新增文章")
    @PostMapping("/article")
    public Result insertArticle(@RequestBody Article article) {
        articleService.save(article);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "查询所有用户")
    @GetMapping("/allUser")
    public Result getUser(@RequestParam("pageNum") Integer pageNum,
                          @RequestParam("pageSize") Integer pageSize) {
        PageVo<SysUser> pageVo = new PageVo<>();
        Page<SysUser> page = new Page<>(pageNum, pageSize);
        List<SysUser> all = sysUserService.page(page).getRecords();
        pageVo.setRecords(all);
        pageVo.setTotal(page.getPages());
        return Result.build(pageVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "根据用户状态查询用户")
    @GetMapping("/user")
    public Result getUser(@RequestParam("status") Integer status,
                          @RequestParam("pageNum") Integer pageNum,
                          @RequestParam("pageSize") Integer pageSize) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getIsDeleted, status);
        Page<SysUser> page = new Page<>(pageNum, pageSize);
        List<SysUser> all = sysUserService.page(page, wrapper).getRecords();
        PageVo<SysUser> pageVo = new PageVo<>();
        pageVo.setRecords(all);
        pageVo.setTotal(page.getPages());
        return Result.build(pageVo, ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "修改用户状态")
    @GetMapping("/status")
    public Result status(@RequestParam("id") Integer id, @RequestParam("status") Integer status) {

        adminService.status(id, status);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "导入单词数据")
    @PostMapping("/import")
    public Result importWord(@RequestParam("file") MultipartFile file,
                             @RequestParam("bookId") Integer bookId) {
        adminService.importWord(file,bookId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

}
